<template>
  <div :class="{fullscreen:fullscreen}" class="tinymce-container editor-container">
    <textarea :id="tinymceId" class="tinymce-textarea" />
    <div class="editor-custom-btn-container">
      <editorImage class="editor-upload-btn" @successCBK="imageSuccessCBK" />
      <editorVideo class="editor-upload-btn" @successVideoCBK="videoSuccessCBK" />
    </div>
  </div>
</template>

<script>
import editorImage from './components/editorImage'
import editorVideo from './components/editorVideo'
import plugins from './plugins'
import toolbar from './toolbar'
export default {
  name: 'Tinymce',
  components: { editorImage, editorVideo },
  props: {
    id: {
      type: String,
      default: () => {
        return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
      }
    },
    value: {
      type: String,
      default: ''
    },
    toolbar: {
      type: Array,
      required: false,
      default() {
        return []
      }
    },
    menubar: {
      type: String,
      default: 'edit insert view format table' // file 隐藏文件菜单
    },
    height: {
      type: Number,
      required: false,
      default: 400
    },
    isOss: {
      type: [String, Boolean],
      required: false,
      default: ''
    }
  },
  data() {
    return {
      hasChange: false,
      hasInit: false,
      tinymceId: this.id,
      fullscreen: false,
      languageTypeList: {
        'zh': 'zh_CN',
        'en': 'en'
      }
    }
  },
  computed: {
    language() {
      return this.languageTypeList[this.$store.getters.language]
    }
  },
  watch: {
    value(val) {
      if (!this.hasChange && this.hasInit) {
        this.$nextTick(() => window.tinymce.get(this.tinymceId).setContent(val || ''))
      }
    },
    language() {
      this.destroyTinymce()
      this.$nextTick(() => this.initTinymce())
    }
  },
  mounted() {
    this.initTinymce()
  },
  activated() {
    this.initTinymce()
  },
  deactivated() {
    this.destroyTinymce()
  },
  destroyed() {
    this.destroyTinymce()
  },
  methods: {
    initTinymce() {
      const _this = this
      window.tinymce.init({
        language: 'zh_CN', // this.language,
        media_live_embeds: true,
        convert_urls: false, // 这个参数加上去就可以了
        automatic_uploads: true,
        selector: `#${this.tinymceId}`,
        height: this.height < 500 ? 500 : this.height,
        min_height: 500,
        body_class: 'panel-body ',
        object_resizing: false,
        toolbar_mode: 'wrap',
        toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
        menubar: this.menubar,
        plugins: plugins,
        lineheight_formats: '1 1.2 1.5 1.8 2 3 4 5',
        fontsize_formats: '8px 10px 12px 16px 14px 18px 24px 36px',
        font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;知乎配置=BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, WenQuanYi Micro Hei, sans-serif;小米配置=Helvetica Neue,Helvetica,Arial,Microsoft Yahei,Hiragino Sans GB,Heiti SC,WenQuanYi Micro Hei,sans-serif',
        end_container_on_empty_block: true,
        powerpaste_word_import: 'clean',
        code_dialog_height: 450,
        code_dialog_width: 1000,
        advlist_bullet_styles: 'square',
        advlist_number_styles: 'default',
        imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
        default_link_target: '_blank',
        link_title: false,
        branding: false,
        elementpath: false,
        nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
        init_instance_callback: editor => {
          if (_this.value) {
            editor.setContent(_this.value)
          }
          _this.hasInit = true
          editor.on('NodeChange Change KeyUp SetContent', () => {
            this.hasChange = true
            this.$emit('input', editor.getContent())
          })
        },
        setup(editor) {
          editor.on('FullscreenStateChanged', (e) => {
            _this.fullscreen = e.state
          })
        }
      })
    },
    destroyTinymce() {
      if (window.tinymce.get(this.tinymceId)) {
        window.tinymce.get(this.tinymceId).destroy()
      }
    },
    setContent(value) {
      window.tinymce.get(this.tinymceId).setContent(value)
    },
    getContent() {
      window.tinymce.get(this.tinymceId).getContent()
    },
    imageSuccessCBK(arr) {
      const _this = this
      arr.forEach(v => {
        window.tinymce.get(_this.tinymceId).insertContent(`<img width="100%" class="wscnph" src="${v}" >`)
      })
    },
    videoSuccessCBK(src) {
      const _this = this
      window.tinymce.get(_this.tinymceId).insertContent(`<div>
          <div
            class="mce-object-video"
            data-mce-p-width="100%"
            data-mce-p-height="auto"
            data-mce-p-controls="controls"
            contenteditable="false"
            data-mce-object="video"
            data-mce-p-controls="controls"
            data-mce-p-allowfullscreen="allowfullscreen"
            data-mce-p-frameborder="no"
            data-mce-p-scrolling="no"
            data-mce-p-src=${src}
            data-mce-html="%20">
              <video width="100%" src=${src} controlslist="nodownload" class="wscnph" controls playsinline></video>
          </div>
        </div>`)
    }
  }
}
</script>

<style scoped>
.tinymce-container {
  position: relative;
}
.tinymce-container >>> .mce-fullscreen {
  z-index: 10000;
}
.tinymce-textarea {
  word-break: break-all;
  visibility: hidden;
  z-index: -1;
}
.editor-custom-btn-container {
  position: absolute;
  right: 4px;
  top: -1px;
  z-index: 2;
}
.fullscreen .editor-custom-btn-container {
  z-index: 3;
  position: fixed;
}
.editor-upload-btn {
  display: inline-block;
}
</style>
